package com.ruoyi.labdata.controller;

import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.labdata.domain.LabFlowInstance;
import com.ruoyi.labdata.service.ILabFlowInstanceService;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 工作流实例Controller
 * 
 * @author winner pan
 * @date 2022-03-11
 */
@RestController
@RequestMapping("/labdata/instance")
public class LabFlowInstanceController extends BaseController {
	@Autowired
	private ILabFlowInstanceService labFlowInstanceService;

	/**
	 * 查询工作流实例列表
	 */
	// @PreAuthorize("@ss.hasPermi('labdata:instance:list')")
	@GetMapping("/finsiedList")
	public TableDataInfo finsiedList(LabFlowInstance labFlowInstance) {
		startPage();
		SysUser loginUser = SecurityUtils.getLoginUser().getUser();
		labFlowInstance.setCreateById(loginUser.getUserId());
		labFlowInstance.setCreateBy(loginUser.getUserName());
		List<LabFlowInstance> list = labFlowInstanceService.selectFinsihedList(labFlowInstance);
		return getDataTable(list);
	}

	// @PreAuthorize("@ss.hasPermi('labdata:instance:list')")
	@GetMapping("/commitedList")
	public TableDataInfo commitedList(LabFlowInstance labFlowInstance) {
		startPage();
		SysUser loginUser = SecurityUtils.getLoginUser().getUser();
		labFlowInstance.setCreateById(loginUser.getUserId());
		labFlowInstance.setCreateBy(loginUser.getUserName());
		List<LabFlowInstance> list = labFlowInstanceService.selectCommittedList(labFlowInstance);
		return getDataTable(list);
	}
	@GetMapping("/myList")
	public TableDataInfo myList(LabFlowInstance labFlowInstance) {
		startPage();
		SysUser loginUser = SecurityUtils.getLoginUser().getUser();
		labFlowInstance.setCreateById(loginUser.getUserId());
		labFlowInstance.setCreateBy(loginUser.getUserName());
		List<LabFlowInstance> list = labFlowInstanceService.selectMyList(labFlowInstance);
		return getDataTable(list);
	}

	@GetMapping("/copyToList")
	public TableDataInfo copyToList(LabFlowInstance labFlowInstance) {
		startPage();
		SysUser loginUser = SecurityUtils.getLoginUser().getUser();
		labFlowInstance.setCreateById(loginUser.getUserId());
		List<LabFlowInstance> list = labFlowInstanceService.selectCopyList(labFlowInstance);
		return getDataTable(list);
	}

	/**
	 * 导出工作流实例列表
	 */
	//@PreAuthorize("@ss.hasPermi('labdata:instance:export')")
	@Log(title = "工作流实例", businessType = BusinessType.EXPORT)
	@GetMapping("/export")
	public AjaxResult export(LabFlowInstance labFlowInstance) {
		List<LabFlowInstance> list = labFlowInstanceService.selectLabFlowInstanceList(labFlowInstance);
		ExcelUtil<LabFlowInstance> util = new ExcelUtil<LabFlowInstance>(LabFlowInstance.class);
		return util.exportExcel(list, "instance");
	}

	/**
	 * 获取工作流实例详细信息
	 */
	//@PreAuthorize("@ss.hasPermi('labdata:instance:query')")
	@GetMapping(value = "/{instanceId}")
	public AjaxResult getInfo(@PathVariable("instanceId") Long instanceId) {
		return AjaxResult.success(labFlowInstanceService.selectLabFlowInstanceById(instanceId));
	}

	/**
	 * 新增工作流实例
	 */
	//@PreAuthorize("@ss.hasPermi('labdata:instance:add')")
	@Log(title = "工作流实例", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody LabFlowInstance labFlowInstance) {
		return toAjax(labFlowInstanceService.insertLabFlowInstance(labFlowInstance));
	}

	/**
	 * 修改工作流实例
	 */
	//@PreAuthorize("@ss.hasPermi('labdata:instance:edit')")
	@Log(title = "工作流实例", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody LabFlowInstance labFlowInstance) {
		return toAjax(labFlowInstanceService.updateLabFlowInstance(labFlowInstance));
	}

	/**
	 * 删除工作流实例
	 */
	//@PreAuthorize("@ss.hasPermi('labdata:instance:remove')")
	@Log(title = "工作流实例", businessType = BusinessType.DELETE)
	@DeleteMapping("/{instanceIds}")
	public AjaxResult remove(@PathVariable Long[] instanceIds) {
		return toAjax(labFlowInstanceService.deleteLabFlowInstanceByIds(instanceIds));
	}
}
